﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using lucky_dog_backend.Models;
using lucky_dog_backend.Services;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace lucky_dog_backend.Controllers
{
    [Route("api/[controller]")]
    public class LuckyTablesController : Controller
    {
        private readonly UserService _userService;
        private readonly LuckyTableService _luckyTableService;
        public LuckyTablesController(UserService userService, LuckyTableService luckyTableService)
        {
            _luckyTableService = luckyTableService;
            _userService = userService;
        }
        [HttpGet]
        public async Task<ActionResult<AsyncDataFormat>> Get() =>
            new AsyncDataSuccessWithData<List<LuckyTableDTO>>
            (LuckyTableListToDTO(await _luckyTableService.Get()));

        [HttpGet("{id:length(24)}")]
        public async Task<ActionResult<AsyncDataFormat>> Get(string id)
        {
            var luckyTable = await _luckyTableService.Get(id);
            if (luckyTable == null)
            {
                return new AsyncDataFaild("表不存在");
            }
            return new AsyncDataSuccessWithData<LuckyTableDTO>(LuckyTableToDTO(luckyTable));
        }
        [HttpPost]
        public async Task<ActionResult<AsyncDataFormat>> Post([FromBody] LuckyTable luckyTable)
        {
            string userId = luckyTable.UserId;
            User user = await _userService.Get(luckyTable.UserId);
            if (luckyTable.UserId==null||userId==""||user==null)
            {
                return new AsyncDataFaild("用户不存在");
            }
            await _luckyTableService.Create(luckyTable);
            return new AsyncDataSuccessWithData<LuckyTableDTO>(LuckyTableToDTO(luckyTable));
        }
        [HttpDelete("{id:length(24)}")]
        public async Task<ActionResult<AsyncDataFormat>> Delete(string id)
        {
            var luckyTable = await _luckyTableService.Get(id);
            if (luckyTable == null)
            {
                return new AsyncDataFaild("表不存在");
            }
            await _luckyTableService.Remove(luckyTable.Id);
            return new AsyncDataSuccessWithoutData();
        }
        public static List<LuckyTableDTO> LuckyTableListToDTO(List<LuckyTable> luckyTableList)
        {
            var tempList = new List<LuckyTableDTO>();
            foreach (var item in luckyTableList)
            {
                tempList.Add(LuckyTableToDTO(item));
            }
            return tempList;
        }
        public static LuckyTableDTO LuckyTableToDTO(LuckyTable luckyTable)
        {
            if (luckyTable.Type == "number")
                return new NumberLuckyTableDTO(luckyTable.Id, luckyTable.Title, luckyTable.Number);
            else
                return new StringLuckyTableDTO(luckyTable.Id, luckyTable.Title, luckyTable.String);
        }
    }
}
